/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.model.templates.event;

import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import java.util.*;

/**
 * @author Rolandas
 */
@XmlType(name = "EventQuestList", propOrder = {"startable", "maintainable"})
@XmlAccessorType(XmlAccessType.FIELD)
public class EventQuestList {

    protected String startable;

    protected String maintainable;

    @XmlTransient
    private List<Integer> startQuests;

    @XmlTransient
    private List<Integer> maintainQuests;

    void afterUnmarshal(Unmarshaller u, Object parent) {
        if (startable != null) {
            startQuests = getQuestsFromData(startable);
        }

        if (maintainable != null) {
            maintainQuests = getQuestsFromData(maintainable);
        }
    }

    List<Integer> getQuestsFromData(String data) {
        Set<String> q = new HashSet<String>();
        Collections.addAll(q, data.split(";"));
        List<Integer> result = new ArrayList<Integer>();

        if (q.size() > 0) {
            result = new ArrayList<Integer>();
            Iterator<String> it = q.iterator();
            while (it.hasNext())
                result.add(Integer.parseInt(it.next()));
        }

        return result;
    }

    /**
     * @return the startQuests (automatically started on logon)
     */
    public List<Integer> getStartableQuests() {
        if (startQuests == null)
            startQuests = new ArrayList<Integer>();
        return startQuests;
    }

    /**
     * @return the maintainQuests (started indirectly from other quests)
     */
    public List<Integer> getMaintainQuests() {
        if (maintainQuests == null)
            maintainQuests = new ArrayList<Integer>();
        return maintainQuests;
    }

}
